<?php
//
//	file: includes/calendar/topics/class_calendar_topics_posting.php
//	author: ptirhiik
//	begin: 30/01/2007
//	version: 1.6.0 - 30/01/2007
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//

if ( !defined('IN_PHPBB') )
{
	die('Hacking attempt');
}

class calendar_topics_posting extends plug_ins
{
	var $calendar_time;
	var $calendar_duration;

	function calendar_topics_posting()
	{
		$this->calendar_time = 0;
		$this->calendar_duration = 0;
	}

	function topic_fields(&$fields)
	{
		$fields[] = 't.topic_calendar_time';
		$fields[] = 't.topic_calendar_duration';
		return true;
	}

	function set_form()
	{
		global $user, $config, $template;
		global $forum_id, $first_post, $post_info;
		global $set_menu, $submit, $resfresh;
		global $error, $error_msg;

		$this->calendar_time = 0;
		$this->calendar_duration = 0;
		if ( ($display_calendar = $first_post && $user->auth(POST_FORUM_URL, 'auth_calendar', $forum_id)) )
		{
			$set_menu++;
			$this->calendar_time = intval($post_info['topic_calendar_time']);
			$calendar_stop = empty($post_info['topic_calendar_duration']) ? 0 : intval($post_info['topic_calendar_time']) + intval($post_info['topic_calendar_duration']);
			$this->calendar_time = empty($this->calendar_time) ? 0 : $user->cvt_sys_to_user_date($this->calendar_time);
			$calendar_stop = empty($calendar_stop) ? 0 : $user->cvt_sys_to_user_date($calendar_stop);
			$fields = array(
				'calendar_title' => array('type' => 'title', 'legend' => 'Calendar_event'),
				'calendar_title_explain' => array('type' => 'comment_light', 'legend' => 'Calendar_event_explain'),
				'calendar_time' => array('type' => 'timestamp', 'legend' => 'Calendar_time', 'value' => $this->calendar_time, 'bold' => true),
				'calendar_stop' => array('type' => 'timestamp', 'legend' => 'Calendar_stop', 'value' => $calendar_stop, 'options.end_date' => true, 'bold' => true),
			);
			$calendar_form = new form($fields, false);
			$calendar_form->process('', ($submit || $refresh), _button('cancel'), 'CALENDAR_FORM');

			// post process
			$this->calendar_time = intval($calendar_form->fields['calendar_time']->value);
			$calendar_stop = intval($calendar_form->fields['calendar_stop']->value);
			$this->calendar_time = ($this->calendar_time <= 0) ? 0 : $user->cvt_user_to_sys_date($this->calendar_time);
			$calendar_stop = ($calendar_stop <= 0) ? 0 : $user->cvt_user_to_sys_date($calendar_stop);

			$this->calendar_duration = 0;
			if ( ($calendar_stop > 0) && (($this->calendar_time == 0) || ($this->calendar_time >= $calendar_stop)) )
			{
				_error('Calendar_date_not_valid');
			}
			else if ( $this->calendar_time > 0 )
			{
				$this->calendar_duration = max(0, $calendar_stop - $this->calendar_time);
			}
			unset($calendar_form);
			$template->assign_vars(array(
				'L_CALENDAR_FORM' => $user->lang('Calendar_event'),
			));
			$template->set_switch('calendar_form', $display_calendar);
		}
		return true;
	}

	function preview()
	{
		global $title_row;
		$title_row = array_merge($title_row, array(
			'topic_calendar_time' => $this->calendar_time,
			'topic_calendar_duration' => $this->calendar_duration,
		));
		return true;
	}

	function submit($new_topic, $first_post, $last_post, &$plug_fields)
	{
		if ( $first_post )
		{
			$plug_fields = array('topic' => array(
				'topic_calendar_time' => intval($this->calendar_time),
				'topic_calendar_duration' => intval($this->calendar_duration),
			));
		}
		return true;
	}
}

?>